package MonotonicStack2;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Stack;

/**
 * @author Lzm
 * @version 1.0
 */
public class nextGreaterElements_ {
  @Test
  public void nextGreaterElementsTest(){
    int[] nums = new int[]{1,2,3};
    int[] newNums = Arrays.copyOf(nums, 2 * nums.length);
    for (int newNum : newNums) {
      System.out.println(newNum);
    }
  }

  public int[] nextGreaterElements(int[] nums) {
    int[] newNums = Arrays.copyOf(nums, 2 * nums.length);
    for (int i = 0; i < nums.length; i++) {
      newNums[i + nums.length] = nums[i];
    }

    Stack<Integer> stack = new Stack<>();
    int[] result = new int[nums.length];
    int[] Result = new int[2 * nums.length];
    Arrays.fill(Result,-1);
    for (int i = 0; i < newNums.length; i++) {
      while(!stack.isEmpty() && newNums[i] > newNums[stack.peek()]){
        Result[stack.peek()] = newNums[i];
        stack.pop();
      }
      stack.push(i);
    }
    for (int i = 0; i < nums.length; i++) {
      result[i] = Result[i];
    }
    return result;
  }
}
